#-------------------------------------------------------------------------------
# Name:       audio_converter_windows.py
# Purpose:    Convert text to audio files
#
# Authors:    Glenn Ramsey <glenn.ramsey@slidespeech.com>
#             John Graves <john.graves@slidespeech.com>
#
# Copyright:  (c) Slidespeech Ltd 2012, 2013
# Licence:    MIT license
# Updated:    3 September 2013
#-------------------------------------------------------------------------------

import os
import subprocess

class AudioConfig:
    ttsURL = "http://www.microsoft.com"
    ttsEngine = "Windows Native"
    ttsExe = "C:\\Windows\\System32\\cscript.exe" # cscript sapi2wav.vbs <filename> <text>
    oggEncoder = "oggenc2.exe" # Generic from http://www.rarewares.org/ogg-oggenc.php
    mp3Encoder = "lame.exe"
    dependencies = [ttsExe,oggEncoder,mp3Encoder]

def maxWorkers(textList):
    # Windows can only run 1 worker
    return 1

def encodeOgg(path, inFileName, outFileName):
    return subprocess.call([AudioConfig.oggEncoder, inFileName, '-o', outFileName])

def encodeMp3(path, inFileName, outFileName):
    return subprocess.call([AudioConfig.mp3Encoder, '-V2', inFileName, outFileName])

def textToAudio(text, language, path, basename):
    filename = os.path.join(path, basename + '.wav')
    ret = subprocess.call([AudioConfig.ttsExe, "sapi2wav.vbs", filename, text.replace("\n"," ")])

    if ret:
        raise RuntimeError("Could not generate audio, {0} failed".format(AudioConfig.ttsExe))

    return filename
